package cms.simmytech.web;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import cms.simmytech.config.KaptchaSingle;
import cms.simmytech.model.SysUser;
import cms.simmytech.service.UserService;
import cms.simmytech.vo.MenuVo;
import cms.simmytech.web.base.BaseController;

@Controller
public class HomeController extends BaseController {

	Log log = LogFactory.getLog(HomeController.class);

	@Autowired
	private UserService userService;

	@RequestMapping("/")
	public String index(Model model) {
		MenuVo menuVo = userService.getMenuPage();
		model.addAttribute("menuVo", menuVo);
		SysUser userBean = userService.getUserInfo();
		model.addAttribute("userBean", userBean);
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		model.addAttribute("year", year);
		return super.toPage("index", model);
	}

	@RequestMapping(value = "/welcome")
	public String welcome(Model model) {
		return super.toPage("welcome", model);
	}

	@RequestMapping("/login")
	public String login(Model model) {
		return super.toPage("login", model);
	}

	@RequestMapping("/defaultKaptcha")
	public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
		try {
			byte[] captchaChallengeAsJpeg = null;
			ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
			try {
				// 代码方式创建:DefaultKaptcha
				KaptchaSingle single = KaptchaSingle.getInstance();
				DefaultKaptcha defaultKaptcha = single.produce();
				// 生产验证码字符串并保存到session中
				String createText = defaultKaptcha.createText();
				httpServletRequest.getSession().setAttribute("vrifyCode", createText);
				// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
				BufferedImage challenge = defaultKaptcha.createImage(createText);
				ImageIO.write(challenge, "jpg", jpegOutputStream);
			}
			catch (IllegalArgumentException e) {
				httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
				return;
			}

			// 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
			captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
			httpServletResponse.setHeader("Cache-Control", "no-store");
			httpServletResponse.setHeader("Pragma", "no-cache");
			httpServletResponse.setDateHeader("Expires", 0);
			httpServletResponse.setContentType("image/jpeg");
			ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
			responseOutputStream.write(captchaChallengeAsJpeg);
			responseOutputStream.flush();
			responseOutputStream.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
